home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / p9030_diag.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  17KB  |  470 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - P9030_DIAG.C
  3. //
  4. // o A simple diagnostics program that lets you access the
  5. //   PLX 9030 registers and local memory. 
  6. // o This program is meant to be used as an example for using the P9030_LIB.H API,
  7. //   you may use it as a skeleton for your driver, or 'cut & paste' parts
  8. //   of it into your device driver code.
  9. // o For a more advanced monitor program, use the standard PLXMON.EXE
  10. //   from PLX.
  11. // 
  12. ////////////////////////////////////////////////////////////////
  13.  
  14. #include "../lib/p9030_lib.h"
  15. #include "../../../samples/shared/pci_diag_lib.h"
  16. #include <stdio.h>
  17.  
  18. // input of command from user
  19. static char line[256];
  20.  
  21. void PLX_EditReg(P9030_HANDLE hPlx)
  22. {
  23.     struct 
  24.     {
  25.         CHAR *name;
  26.         DWORD dwOffset;
  27.         DWORD dwVal;
  28.     } fields[30];
  29.  
  30.     int cmd;
  31.     int i;
  32.     int field_count;
  33.  
  34.     i = 0;
  35.     fields[i].name = "LAS0RR"; fields[i++].dwOffset = 0x00;
  36.     fields[i].name = "LAS1RR"; fields[i++].dwOffset = 0x04;
  37.     fields[i].name = "LAS2RR"; fields[i++].dwOffset = 0x08;
  38.     fields[i].name = "LAS3RR"; fields[i++].dwOffset = 0x0c;
  39.     fields[i].name = "EROMRR"; fields[i++].dwOffset = 0x10;
  40.     fields[i].name = "LAS0BA"; fields[i++].dwOffset = 0x14;
  41.     fields[i].name = "LAS1BA"; fields[i++].dwOffset = 0x18;
  42.     fields[i].name = "LAS2BA"; fields[i++].dwOffset = 0x1c;
  43.     fields[i].name = "LAS3BA"; fields[i++].dwOffset = 0x20;
  44.     fields[i].name = "EROMBA"; fields[i++].dwOffset = 0x24;
  45.     fields[i].name = "LAS0BRD"; fields[i++].dwOffset = 0x28;
  46.     fields[i].name = "LAS1BRD"; fields[i++].dwOffset = 0x2c;
  47.     fields[i].name = "LAS2BRD"; fields[i++].dwOffset = 0x30;
  48.     fields[i].name = "LAS3BRD"; fields[i++].dwOffset = 0x34;
  49.     fields[i].name = "EROMBRD"; fields[i++].dwOffset = 0x38;
  50.     fields[i].name = "CS0BASE"; fields[i++].dwOffset = 0x3c;
  51.     fields[i].name = "CS1BASE"; fields[i++].dwOffset = 0x40;
  52.     fields[i].name = "CS2BASE"; fields[i++].dwOffset = 0x44;
  53.     fields[i].name = "CS3BASE"; fields[i++].dwOffset = 0x48;
  54.     fields[i].name = "INTCSR"; fields[i++].dwOffset = 0x4c;
  55.     fields[i].name = "CNTRL"; fields[i++].dwOffset = 0x50;
  56.     fields[i].name = "GPIOC"; fields[i++].dwOffset = 0x54;
  57.  
  58.     field_count = i;
  59.     do
  60.     {
  61.         int row;
  62.         int col;
  63.         int row_count = field_count/2 + field_count%2;
  64.  
  65.         printf ("\n");
  66.         printf ("Edit PLX 9030 registers\n");
  67.         printf ("--------------------------------\n");
  68.         for (row = 0; row<row_count; row++)
  69.         {
  70.             for (col = 0; col<=1; col++)
  71.             {
  72.                 if (col==0) i = row;
  73.                 else i = row + row_count;
  74.  
  75.                 if (i<field_count)
  76.                 {
  77.                     char buf[10];
  78.                     fields[i].dwVal = P9030_ReadReg(hPlx, fields[i].dwOffset);
  79.                     sprintf(buf, "%08x",fields[i].dwVal);
  80.                     printf ("%2d. %12s : %s     ",i+1, fields[i].name, buf);
  81.                 }
  82.                 if (col==1) printf ("\n");
  83.             }
  84.         }
  85.  
  86.         printf ("99. Back to main menu\n");
  87.         printf ("Choose register to write to, or 99 to exit: ");
  88.         cmd = 0;
  89.         fgets(line, sizeof(line), stdin);
  90.         sscanf (line, "%d",&cmd);
  91.         if (cmd>=1 && cmd <=field_count)
  92.         {
  93.             i = cmd-1;
  94.             printf ("Enter value to write to %s register (or 'X' to cancel): ",fields[i].name);
  95.             fgets(line, sizeof(line), stdin);
  96.             if (toupper (line[0])!='X')
  97.             {
  98.                 DWORD dwVal;
  99.                 dwVal = 0;
  100.                 sscanf (line,"%x",&dwVal);
  101.                 P9030_WriteReg(hPlx, fields[i].dwOffset, dwVal);
  102.             }
  103.         }
  104.     } while (cmd!=99);
  105. }
  106.  
  107. char *PLX_GetAddrRangeName(P9030_ADDR addrSpace)
  108. {
  109.     return 
  110.         addrSpace==P9030_ADDR_SPACE0 ? "Addr Space 0 - (BAR2)" :
  111.         addrSpace==P9030_ADDR_SPACE1 ? "Addr Space 1 - (BAR3)" :
  112.         addrSpace==P9030_ADDR_SPACE2 ? "Addr Space 2 - (BAR4)" :
  113.         addrSpace==P9030_ADDR_SPACE3 ? "Addr Space 3 - (BAR5)" :
  114.         addrSpace==P9030_ADDR_EPROM ? "EEPROM Addr Space" : "Invalid";
  115. }
  116.  
  117. void PLX_BoardAccess(P9030_HANDLE hPlx, BOOL fLocalAddr)
  118. {
  119.     int cmd, cmd2, i;
  120.     DWORD addr, data;
  121.     P9030_ADDR ad_sp = P9030_ADDR_SPACE0;
  122.     P9030_MODE ad_mode = P9030_MODE_DWORD;
  123.     char *pcMemoryType = fLocalAddr ? "local address" : "offset";
  124.  
  125.     for (; ad_sp<=P9030_ADDR_EPROM && !P9030_IsAddrSpaceActive(hPlx, ad_sp); ad_sp++)
  126.     if (ad_sp>P9030_ADDR_EPROM)
  127.     {
  128.         printf ("No active memory spaces on board!\n");
  129.         return;
  130.     }
  131.  
  132.     do
  133.     {
  134.         printf ("Access the board's %s ranges\n",pcMemoryType);
  135.         printf ("-------------------------------------------\n");
  136.         printf ("(Access to invalid %s may hang the computer!)\n", pcMemoryType);
  137.         printf ("1. Change active memory space: %s\n",PLX_GetAddrRangeName(ad_sp));
  138.         printf ("2. Toggle active mode: %s\n", 
  139.             ad_mode==P9030_MODE_BYTE ? "BYTE (8 bit)" :
  140.             ad_mode==P9030_MODE_WORD ? "WORD (16 bit)" : "DWORD (32 bit)");
  141.         printf ("3. Read from board\n");
  142.         printf ("4. Write to board\n");
  143.         printf ("99. Back to main menu\n");
  144.         printf ("\n");
  145.         printf ("Enter option: ");
  146.         cmd = 0;
  147.         fgets(line, sizeof(line), stdin);
  148.         sscanf (line, "%d",&cmd);
  149.         switch (cmd)
  150.         {
  151.         case 1:
  152.             printf ("Choose memory space:\n");
  153.             printf ("--------------------\n");
  154.             for (i=P9030_ADDR_SPACE0; i<=P9030_ADDR_EPROM; i++)
  155.             {
  156.                 printf ("%d. %s", i, PLX_GetAddrRangeName(i));
  157.                 if (P9030_IsAddrSpaceActive(hPlx, i)) printf ("\n");
  158.                 else printf (" - space not active\n");
  159.             }
  160.             printf ("Enter option: ");
  161.             cmd2 = 99;
  162.             fgets(line, sizeof(line), stdin);
  163.             sscanf (line, "%d",&cmd2);
  164.             if (cmd2>=P9030_ADDR_SPACE0 && cmd2<=P9030_ADDR_EPROM)
  165.             {
  166.                 int new_ad_sp = cmd2;
  167.                 if (P9030_IsAddrSpaceActive(hPlx, new_ad_sp)) ad_sp = new_ad_sp;
  168.                 else printf ("Chosen space not active!\n");
  169.             }
  170.             break;
  171.         case 2:
  172.             ad_mode = (ad_mode + 1) % 3;
  173.             break;
  174.         case 3:
  175.             printf ("Enter %s to read from: ", pcMemoryType);
  176.             fgets(line, sizeof(line), stdin);
  177.             sscanf (line, "%x", &addr);
  178.             switch (ad_mode)
  179.             {
  180.             case P9030_MODE_BYTE:
  181.                 if (fLocalAddr) data = P9030_ReadByte(hPlx, ad_sp, addr);
  182.                 else data = P9030_ReadSpaceByte(hPlx, ad_sp, addr);
  183.                 break;
  184.             case P9030_MODE_WORD:
  185.                 if (fLocalAddr) data = P9030_ReadWord(hPlx, ad_sp, addr);
  186.                 else data = P9030_ReadSpaceWord(hPlx, ad_sp, addr);
  187.                 break;
  188.             case P9030_MODE_DWORD:
  189.                 if (fLocalAddr) data = P9030_ReadDWord(hPlx, ad_sp, addr);
  190.                 else data = P9030_ReadSpaceDWord(hPlx, ad_sp, addr);
  191.                 break;
  192.             }
  193.             printf ("Value read: %x\n", data);
  194.             break;
  195.         case 4:
  196.             printf ("Enter %s to write to: ", pcMemoryType);
  197.             fgets(line, sizeof(line), stdin);
  198.             sscanf (line, "%x", &addr);
  199.             printf ("Enter data to write %s: ",
  200.                 ad_mode==P9030_MODE_BYTE ? "BYTE (8 bit)" :
  201.                 ad_mode==P9030_MODE_WORD ? "WORD (16 bit)" : "DWORD (32 bit)");
  202.             fgets(line, sizeof(line), stdin);
  203.             sscanf (line, "%x",&data);
  204.             switch (ad_mode)
  205.             {
  206.             case P9030_MODE_BYTE:
  207.                 if (fLocalAddr) P9030_WriteByte(hPlx, ad_sp, addr, (BYTE) data);
  208.                 else P9030_WriteSpaceByte(hPlx, ad_sp, addr, (BYTE) data);
  209.                 break;
  210.             case P9030_MODE_WORD:
  211.                 if (fLocalAddr) P9030_WriteWord(hPlx, ad_sp, addr, (WORD) data);
  212.                 else P9030_WriteSpaceWord(hPlx, ad_sp, addr, (WORD) data);
  213.                 break;
  214.             case P9030_MODE_DWORD:
  215.                 if (fLocalAddr) P9030_WriteDWord(hPlx, ad_sp, addr, data);
  216.                 else P9030_WriteSpaceDWord(hPlx, ad_sp, addr, data);
  217.                 break;
  218.             }
  219.             break;
  220.         }
  221.     } while (cmd!=99);
  222. }
  223.  
  224. void WINAPI PLX_IntHandlerRoutine(P9030_HANDLE hPlx, P9030_INT_RESULT *intResult)
  225. {
  226.     printf ("Got interrupt number %d\n", intResult->dwCounter);
  227. }
  228.  
  229. void PLX_EnableDisableInterrupts(P9030_HANDLE hPlx)
  230. {
  231.     int cmd;
  232.  
  233.     printf ("WARNING!!!\n");
  234.     printf ("----------\n");
  235.     printf ("Your hardware has level sensitive interrupts.\n");
  236.     printf ("You must modify the source code of P9030_IntEnable(), in the file p9030_lib.c,\n");
  237.     printf ("to acknowledge the interrupt before enabling interrupts.\n");
  238.     printf ("Without this modification, your PC will HANG upon interrupt!\n");
  239.  
  240.     do
  241.     {
  242.         printf ("Enable / Disable interrupts\n");
  243.         printf ("---------------------------\n");
  244.         printf ("1. %s interrupts\n", P9030_IntIsEnabled(hPlx) ? "Disable" : "Enable");
  245.         printf ("99. Back to main menu\n");
  246.         printf ("\n");
  247.         printf ("Enter option: ");
  248.         cmd = 0;
  249.         fgets(line, sizeof(line), stdin);
  250.         sscanf (line, "%d",&cmd);
  251.         switch (cmd)
  252.         {
  253.         case 1:
  254.             if (P9030_IntIsEnabled(hPlx))
  255.             {
  256.                 printf ("Disabling interrupt Int\n");
  257.                 P9030_IntDisable(hPlx);
  258.             }
  259.             else
  260.             {
  261.                 printf ("Enabling interrupts\n");
  262.                 if (!P9030_IntEnable(hPlx, PLX_IntHandlerRoutine))
  263.                     printf ("failed enabling interrupts\n");
  264.             }
  265.             break;
  266.         }
  267.     } while (cmd!=99);
  268. }
  269.  
  270. void PLX_EEPROMAccess(P9030_HANDLE hPlx)
  271. {
  272.     int cmd;
  273.     DWORD addr;
  274.     DWORD dwData;
  275.  
  276.     do
  277.     {
  278.         printf ("Access the board's serial EERPOM\n");
  279.         printf ("--------------------------------\n");
  280.         if (!P9030_EEPROMValid(hPlx))
  281.             printf ("Note: PLX EEPROM valid BIT is 0\n");
  282.         printf ("1. Display EEPROM content\n");
  283.         printf ("2. Read dword from serial EEPROM on the board\n");
  284.         printf ("3. Write dword to the serial EEPROM on the board\n");
  285.         printf ("99. Back to main menu\n");
  286.         printf ("\n");
  287.         printf ("Enter option: ");
  288.         cmd = 0;
  289.         fgets(line, sizeof(line), stdin);
  290.         sscanf (line, "%d",&cmd);
  291.         switch (cmd)
  292.         {
  293.         case 1:
  294.             for (addr=0; addr<0xff; addr += 4)
  295.             {
  296.                 if (!(addr % 0x10))
  297.                 printf("\n %02x: ", addr);
  298.                 if (!P9030_EEPROMReadDWord(hPlx, addr, &dwData))
  299.                 {
  300.                     printf("\nError occured reading serial EEPROM - %s\n", P9030_ErrorString);
  301.                     break;
  302.                 }
  303.                 printf("%08x  ", dwData);
  304.             }
  305.             printf ("\n");
  306.             break;
  307.         case 2:
  308.             printf ("Enter addr to read from (0-7f): ");
  309.             fgets(line, sizeof(line), stdin);
  310.             sscanf (line, "%x", &addr);
  311.             if (P9030_EEPROMReadDWord(hPlx, addr, &dwData))
  312.                 printf ("Value read: %08x\n", dwData);
  313.              else
  314.                 printf("Error occured reading serial EEPROM - %s\n", P9030_ErrorString);
  315.             break;
  316.  
  317.         case 3:
  318.             printf ("Enter addr to write to (0-7f): ");
  319.             fgets(line, sizeof(line), stdin);
  320.             sscanf (line, "%x", &addr);
  321.             printf ("Enter data to write: ");
  322.             fgets(line, sizeof(line), stdin);
  323.             sscanf (line, "%x",&dwData);
  324.             if (!P9030_EEPROMWriteDWord(hPlx, addr, dwData))
  325.                 printf("Error occured reading serial EEPROM - %s\n", P9030_ErrorString);
  326.  
  327.             break;
  328.  
  329.         default:
  330.             break;
  331.         }
  332.     } while (cmd!=99);
  333. }
  334.  
  335. P9030_HANDLE PLX_LocateAndOpenBoard(DWORD dwVendorID, DWORD dwDeviceID, BOOL fUseInt)
  336. {
  337.     DWORD cards, my_card;
  338.     P9030_HANDLE hPlx = NULL;
  339.  
  340.     if (dwVendorID==0)
  341.     {
  342.         printf ("Enter VendorID: ");
  343.         fgets(line, sizeof(line), stdin);
  344.         sscanf (line, "%x",&dwVendorID);
  345.         if (dwVendorID==0) return NULL;
  346.  
  347.         printf ("Enter DeviceID: ");
  348.         fgets(line, sizeof(line), stdin);
  349.         sscanf (line, "%x",&dwDeviceID);
  350.     }
  351.     cards = P9030_CountCards (dwVendorID, dwDeviceID);
  352.     if (cards==0) 
  353.     {
  354.         printf("%s", P9030_ErrorString);
  355.         return NULL;
  356.     }
  357.     else if (cards==1) my_card = 1;
  358.     else
  359.     {
  360.         DWORD i;
  361.  
  362.         printf("Found %d matching PCI cards\n", cards);
  363.         printf("Select card (1-%d): ", cards);
  364.         i = 0;
  365.         fgets(line, sizeof(line), stdin);
  366.         sscanf (line, "%d",&i);
  367.         if (i>=1 && i <=cards) my_card = i;
  368.         else 
  369.         {
  370.             printf ("Choice out of range\n");
  371.             return NULL;
  372.         }
  373.     }
  374.     if (P9030_Open (&hPlx, dwVendorID, dwDeviceID, my_card - 1, fUseInt ? P9030_OPEN_USE_INT : 0))
  375.         printf ("PLX 9030 PCI card found!\n");
  376.     else printf ("%s", P9030_ErrorString);
  377.     return hPlx;
  378. }
  379.  
  380. int main(int argc, char *argv[])
  381. {
  382.     int cmd;
  383.     P9030_HANDLE hPlx = NULL;
  384.     HANDLE hWD;
  385.     BOOL fUseInt = FALSE; // by default - do not install interrupts
  386.     BOOL fOpenedWithInt = fUseInt;
  387.     
  388.     printf ("PLX 9030 diagnostic utility.\n");
  389.     printf ("Application accesses hardware using " WD_PROD_NAME ".\n");
  390.  
  391.     // make sure WinDriver is loaded
  392.     if (!PCI_Get_WD_handle(&hWD)) return 0;
  393.     WD_Close (hWD);
  394.  
  395.     hPlx = PLX_LocateAndOpenBoard(0x10b5, 0x9030, fUseInt);
  396.  
  397.     do
  398.     {
  399.         printf ("\n");
  400.         printf ("PLX 9030 main menu\n");
  401.         printf ("-------------------\n");
  402.         printf ("1. Scan PCI bus\n");
  403.         printf ("2. Set opening board %s interrupts\n", fUseInt ? "without" : "with");
  404.         printf ("3. Locate/Choose PLX 9030 board (%s interrupts)\n", fUseInt ? "with" : "without");
  405.         if (hPlx)
  406.         {
  407.             DWORD dwCNTRL = P9030_ReadReg(hPlx, P9030_CNTRL);
  408.             dwCNTRL |= BIT29;
  409.             P9030_WriteReg( hPlx, P9030_CNTRL, dwCNTRL);
  410.  
  411.             printf ("4. PCI configuration registers\n");
  412.             printf ("5. PLX 9030 local registers\n");
  413.             printf ("6. Access address spaces on the board\n");
  414.             printf ("7. Access local address ranges on the board\n");
  415.             if (fOpenedWithInt)
  416.                 printf ("8. Enable / Disable interrupts\n");
  417.             printf ("9. Access serial EEPROM on the board\n");
  418.         }
  419.         printf ("99. Exit\n");
  420.         printf ("Enter option: ");
  421.         cmd = 0;
  422.         fgets(line, sizeof(line), stdin);
  423.         sscanf (line, "%d",&cmd);
  424.         switch (cmd)
  425.         {
  426.         case 1: // Scan PCI bus
  427.             PCI_Print_all_cards_info();
  428.             break;
  429.         case 2: // Set open board with / without interrupts
  430.             fUseInt = !fUseInt;
  431.             break;
  432.         case 3: // Locate PLX 9030 board
  433.             if (hPlx) P9030_Close(hPlx);
  434.             hPlx = PLX_LocateAndOpenBoard(0, 0, fUseInt);
  435.             if (!hPlx) printf ("PLX card open failed!\n");
  436.             fOpenedWithInt = fUseInt;
  437.             break;
  438.         case 4: // PCI configuration registers
  439.             if (hPlx) 
  440.             {
  441.                 WD_PCI_SLOT pciSlot;
  442.                 P9030_GetPciSlot(hPlx, &pciSlot);
  443.                 PCI_EditConfigReg(pciSlot);
  444.             }
  445.             break;
  446.         case 5: // PLX 9030 local registers
  447.             if (hPlx) PLX_EditReg(hPlx);
  448.             break;
  449.         case 6: // Access address spaces on the board
  450.             if (hPlx) PLX_BoardAccess(hPlx, FALSE);
  451.             break;
  452.         case 7: // Access local address ranges on the board
  453.             if (hPlx) PLX_BoardAccess(hPlx, TRUE);
  454.             break;
  455.         case 8: // Enable / Disable interrupts
  456.             if (hPlx && fOpenedWithInt) PLX_EnableDisableInterrupts(hPlx);
  457.             break;
  458.         case 9: // Access serial EEPROM on the board
  459.             if (hPlx) PLX_EEPROMAccess(hPlx);
  460.             break;
  461.         }
  462.     } while (cmd!=99);
  463.  
  464.     if (hPlx) P9030_Close(hPlx);
  465.  
  466.     return 0;
  467. }
  468.  
  469.                                       
  470.